package com.ibm.epss.android;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class PkgExpMsgRevHandler extends Handler {
    private PendingIntent pt;
    private ActivityProfile activityProfile = ActivityProfile.getInstance();

    public PkgExpMsgRevHandler() {
        super();
        Notification nf = new Notification();
        nf.icon = R.drawable.ic_demo_app;
        nf.tickerText = "New message received";
        nf.defaults = Notification.DEFAULT_ALL;
        nf.flags |= Notification.FLAG_AUTO_CANCEL;
        activityProfile.setNotification(nf);
    }

    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        activityProfile.getReceivedMsgs().add((String) msg.obj);
        Log.d("MQTT", "get msg - " + (String) msg.obj);
        showNotificationMsg(msg);
    }

    private void showNotificationMsg(Message msg) {
        // Show a notification
        Intent intent = new Intent();
        intent.setClass(activityProfile.getCtxActivity(), PackageExpressNotificationActivity.class);
        pt = PendingIntent.getActivity(activityProfile.getCtxActivity(), 0, intent, 0);

        activityProfile.getNotification().when = System.currentTimeMillis();
        activityProfile.getNotification().setLatestEventInfo(activityProfile.getCtxService(), "New message", " new message(s)", pt);
        activityProfile.getNotiMgr().notify(activityProfile.getNotificationID(), activityProfile.getNotification());
        if (activityProfile.getTvMsg() != null) {
            activityProfile.getTvMsg().append(msg.obj + "\n");
        }
    }
}
